package com.ocom.webapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.MapKey;
import com.ocom.common.dto.Department;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.recode.AccessRecordVo;
import com.ocom.webapi.feign.AccessClientService;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.feign.MjRecodeClientService;
import com.ocom.webapi.service.QuarantinePersonService;
import com.ocom.webapi.vo.quarantineperson.OnShowVo;
import com.ocom.webapi.vo.quarantineperson.YkmSumVo;
import com.ocom.webapi.vo.quarantineperson.YmDataGroupByMachVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class QuarantinePersonServiceImpl implements QuarantinePersonService {
    private final Logger log = LoggerFactory.getLogger(QuarantinePersonService.class);

    @Autowired
    private AccessClientService accessClientService;


    @Autowired
    private HumanClientService humanClientService;

    @Autowired
    private MjRecodeClientService mjRecodeClientService;

    /**
     * 防疫人员列表（QuarantinePerson/onShow）
     * */
    @Override
    public Result QuarantinePersonOnShow(OnShowVo onShowVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),onShowVo.getComId());
        if (onShowVo.getDeptId()!=null && onShowVo.getDeptId()>-1){
            map.put(mapKey.getKey_deptIds(),onShowVo.getDeptId());
        }
        if (StringUtils.isNotEmpty(onShowVo.getPName())){
            map.put(mapKey.getKey_pName(),onShowVo.getPName());
        }
        if (StringUtils.isNotEmpty(onShowVo.getPCode())){
            map.put(mapKey.getKey_pCode(),onShowVo.getPCode());
        }
        if (StringUtils.isNotNull(onShowVo.getCodeColor())){
            map.put(mapKey.getKey_codeColor(),onShowVo.getCodeColor());
        }
        if (StringUtils.isNotNull(onShowVo.getHsResult())){
            map.put(mapKey.getKey_hsResult(),onShowVo.getHsResult());
        }

        map.put(mapKey.getKey_page(),onShowVo.getPage());
        map.put(mapKey.getKey_size(),onShowVo.getSize());
        return accessClientService.GetYkmPersonnelList(map);
    }

    /**
     * 防疫汇总指标列表（QuarantinePerson/onYkmSum）
     * */
    @Override
    public Result QuarantinePersonYkmSum(YkmSumVo ykmSumVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();

        if (ykmSumVo.getDeptDown().equals(0)){
            map.put(mapKey.getKey_comId(),ykmSumVo.getComId());
            if (ykmSumVo.getDeptId()!=null && ykmSumVo.getDeptId()>-1){
                map.put(mapKey.getKey_deptIds(),ykmSumVo.getDeptId());
            }
            map.put(mapKey.getKey_pName(),ykmSumVo.getPName());
            map.put(mapKey.getKey_pCode(),ykmSumVo.getPCode());
            map.put(mapKey.getKey_paraSum(),ykmSumVo.getParaSum());
            map.put(mapKey.getKey_page(),ykmSumVo.getPage());
            map.put(mapKey.getKey_size(),ykmSumVo.getSize());
            log.info("防疫汇总指标列表:map{{}}",map);
            return   accessClientService.getYkmSumList(map);
        }else {
            if (ykmSumVo.getDeptId()!=null && ykmSumVo.getDeptId() > -1){
                Department department = new Department();
                department.setComId(ykmSumVo.getComId());
                department.setDeptId(ykmSumVo.getDeptId());
                department.setShowLevel(0);

                log.info("防疫汇总指标列表:department{{}}",department);
                Result result = humanClientService.getDeptIds(department);
                if (result.getCode()!=200){
                    return ResultUtil.error(result.getCode());
                }
                log.info("防疫汇总指标列表部门id:result{{}}",result);
                map.put(mapKey.getKey_deptIds(), JSON.parseObject(JSON.toJSONString(result.getData())).getString("deptIds")+","+ykmSumVo.getDeptId());
            }
            map.put(mapKey.getKey_comId(),ykmSumVo.getComId());
            map.put(mapKey.getKey_pName(),ykmSumVo.getPName());
            map.put(mapKey.getKey_pCode(),ykmSumVo.getPCode());
            map.put(mapKey.getKey_paraSum(),ykmSumVo.getParaSum());
            map.put(mapKey.getKey_page(),ykmSumVo.getPage());
            map.put(mapKey.getKey_size(),ykmSumVo.getSize());
            log.info("防疫汇总指标列表:map{{}}",map);
            return  accessClientService.getYkmSumList(map);
        }
    }
    /**
     * 防疫人员核酸变化记录表（QuarantinePerson/hsResultList）
     * */
    @Override
    public Result QuarantinePersonHsResultList(YkmSumVo ykmSumVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();

        if (ykmSumVo.getDeptDown().equals(0)){
            map.put(mapKey.getKey_comId(),ykmSumVo.getComId());
            if (ykmSumVo.getDeptId()!=null && ykmSumVo.getDeptId()>-1){
                map.put(mapKey.getKey_deptIds(),ykmSumVo.getDeptId());
            }
            map.put(mapKey.getKey_pName(),ykmSumVo.getPName());
            map.put(mapKey.getKey_pCode(),ykmSumVo.getPCode());
            map.put(mapKey.getKey_paraSum(),ykmSumVo.getParaSum());
            map.put(mapKey.getKey_page(),ykmSumVo.getPage());
            map.put(mapKey.getKey_size(),ykmSumVo.getSize());
            log.info("防疫人员核酸变化记录表:map{{}}",map);
            return   accessClientService.getYkmSumList(map);
        }else {
            if (ykmSumVo.getDeptId()!=null && ykmSumVo.getDeptId() > -1){
                Department department = new Department();
                department.setComId(ykmSumVo.getComId());
                department.setDeptId(ykmSumVo.getDeptId());
                department.setShowLevel(0);

                log.info("防疫人员核酸变化记录表部门:department{{}}",department);
                Result result = humanClientService.getDeptIds(department);
                if (result.getCode()!=200){
                    return ResultUtil.error(result.getCode());
                }
                log.info("防疫人员核酸变化记录表部门id:result{{}}",result);
                map.put(mapKey.getKey_deptIds(), JSON.parseObject(JSON.toJSONString(result.getData())).getString("deptIds")+","+ykmSumVo.getDeptId());
            }
            map.put(mapKey.getKey_comId(),ykmSumVo.getComId());
            map.put(mapKey.getKey_pName(),ykmSumVo.getPName());
            map.put(mapKey.getKey_pCode(),ykmSumVo.getPCode());
            map.put(mapKey.getKey_paraSum(),ykmSumVo.getParaSum());
            map.put(mapKey.getKey_page(),ykmSumVo.getPage());
            map.put(mapKey.getKey_size(),ykmSumVo.getSize());
            log.info("防疫人员核酸变化记录表:map{{}}",map);
            return  accessClientService.getYkmSumList(map);
        }
    }

    /**
     *  防疫门禁列表（QuarantineRecord/onShow）
     * */
    @Override
    public Result QuarantineRecordOnShow(AccessRecordVo accessRecordVo) {
        return mjRecodeClientService.GetAccessRecord(accessRecordVo);
    }

    /**
     * 区域统计列表（QuarantineAreaRecord/onShow）
     * */
    @Override
    public Result QuarantineAreaRecordOnShow(YmDataGroupByMachVo ymDataGroupByMachVo) {
        return mjRecodeClientService.getYmDataGroupByMach(ymDataGroupByMachVo);
    }
}
